﻿@page "/file share"
@using System.Text
@using Microsoft.Extensions.Options
@using Tmp.Models
@inject NotificationService NotificationService;

<RadzenCard>
    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">分享文件</RadzenText>
    <RadzenUpload @ref="upload" Auto="false"
                  ChooseText="选择文件"
                  Multiple="true"
                  Change="@(OnChange)"
                  Progress="@(OnProgress)" class="w-100" />
    <RadzenProgressBar Value=@_progress class="mt-4" Visible=@_showProgress />
    <RadzenButton Text="Upload" Click="@(async _ => await UploadFile())" class="d-block mt-4" />
    </RadzenCard>
@code {

    [Inject]
    IOptions<Configuration> _options { get; set; }


    [Inject]
    NavigationManager? Manager { get; set; }
    RadzenUpload? upload;
    int _progress;
    bool _showProgress;
    IEnumerable<FileInfo>? _files;
    void OnChange(UploadChangeEventArgs args)
    {
        _files = args.Files;
    }
    void OnProgress(UploadProgressArgs args)
    {
        _progress = args.Progress;
    }

    private async Task UploadFile()
    {
        _progress = 0;
        if (_files==null|| !_files.Any())
        {
            NotificationService.Notify(NotificationSeverity.Warning, "提示", "没有选择文件");
            //提示没有选择文件
            return;
        }
        foreach (var fileInfo in _files)
        {
            if (fileInfo.Size > _options.Value.MaxFileSize)
            {
                NotificationService.Notify(NotificationSeverity.Warning, "提示", "有文件超过了限定大小");
                // 提示有文件超过了限定大小
                return;
            }
        }

        _showProgress = true;
        var builder = new StringBuilder();
        if (_files != null && _files.Any())
        {
            foreach (var fileInfo in _files)
            {
                builder.Append(fileInfo.Name+fileInfo.Size);
            }
        }
        // builder.Append(DateTime.Now.Hour);
        var hash = builder.ToString().GetHashCode();
        upload.Url = $"upload/multiple/{hash}";
        await upload.Upload();
        NotificationService.Notify(NotificationSeverity.Success, "提示", "文件上传成功");
        Manager.NavigateTo($"/ShareResult/{hash}?type=files");
    }

}